<template>
  <div class="box">
    <header class="header">
        <van-nav-bar
          title="我的订单"
          left-arrow
          @click-left="$router.push('/my')"
        />
    </header>
    <div class="content order" ref="cont">
      <van-tabs v-model="active">
        <van-tab title="全部订单">
            <van-cell-group v-for="item in orderList" :key="item.orderId">
              <van-cell title="订单编号" :value="item.orderId"/>
              <van-cell>
                <template #title>
                    <div class="recipient">收件人：<span>石涛</span></div>
                    <div class="recipient">收货地址：<span>上海市宝山区同济支路542号XXX小区</span></div>
                    <div class="recipient">配送时间：<span>2020-10-17 16:30-17:30</span></div>
                </template>
                <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
              </van-cell>
              <van-card
                v-for="val in item.proData"
                :key="val.proId"
                :num="val.num"
                :price="val.price"
                :title="val.proName"
                :thumb="val.proImg"
              />
              <van-cell>
                <template #title>
                    <div class="recipient">合计：<span>￥{{orderSumPrice(item.orderId)}}</span></div>
                </template>
                <van-button class="evaluate" size="mini" @click="evaluation(item.orderId)">给与评价</van-button>
              </van-cell>
            </van-cell-group>
        </van-tab>
        <van-tab title="待付款">
          <div class="order" v-for="item in orderList1" :key="item.proId">
              <div class="cont" @click="orderDetail(item.orderId)">
                <h3 class="orderNum" ><span class="num">订单编号：{{item.orderId}}</span><span class="statePay">{{flag1}}</span></h3>
                <van-card
                  v-for="val in item.proData"
                  :key="val.proId"
                  :num="val.num"
                  :price="val.price"
                  :title="val.proName"
                  :thumb="val.proImg"
                />
              </div>
              <div class="order-footer">
                <!-- 计算属性无法传参 -->
                <!-- <van-button type="danger" class="delOrder" size="mini" @click="delOrder(item.orderId)">取消订单</van-button> -->
                <van-button type="danger" class="delOrder" size="mini" @click="cancelOrder(item.orderId)">取消订单</van-button>
                <div class="pay-right">
                    <span class="order-sum">合计:<i class="order-money">￥{{orderSumPrice(item.orderId)}}</i></span>
                    <van-button type="danger" class="gopay" size="mini" @click="goPay(item.orderId)">去支付</van-button>
                </div>
              </div>
          </div>
          <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
        </van-tab>
        <van-tab title="待配送">
          <div class="order" v-for="item in orderList2" :key="item.proId">
              <div class="cont"  @click="orderDetail(item.orderId)">
                <h3 class="orderNum" ><span class="num">订单编号：{{item.orderId}}</span><span class="statePay">{{flag2}}</span></h3>
                <van-card
                  v-for="val in item.proData"
                  :key="val.proId"
                  :num="val.num"
                  :price="val.price"
                  :title="val.proName"
                  :thumb="val.proImg"
                />
              </div>
          </div>
          <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
        </van-tab>
        <van-tab title="待签收">
            <div class="order" v-for="item in orderList3" :key="item.proId">
                <div class="cont"  @click="orderDetail(item.orderId)">
                  <h3 class="orderNum" ><span class="num">订单编号：{{item.orderId}}</span><span class="statePay">{{flag3}}</span></h3>
                  <van-card
                    v-for="val in item.proData"
                    :key="val.proId"
                    :num="val.num"
                    :price="val.price"
                    :title="val.proName"
                    :thumb="val.proImg"
                  />
                </div>
                <div class="order-footer">
                  <!-- 计算属性无法传参 -->
                  <div></div>
                  <div class="sign-right">
                      <van-button type="primary" size="small" @click="goSign(item.orderId)">确认签收</van-button>
                  </div>
                </div>
            </div>
            <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
        </van-tab>
        <van-tab title="已结束">
            <div class="order" v-for="item in orderList5" :key="item.proId">
                <div class="cont"  @click="orderDetail(item.orderId)">
                  <h3 class="orderNum" ><span class="num">订单编号：{{item.orderId}}</span><span class="statePay">{{flag4}}</span></h3>
                  <van-card
                    v-for="val in item.proData"
                    :key="val.proId"
                    :num="val.num"
                    :price="val.price"
                    :title="val.proName"
                    :thumb="val.proImg"
                  />
                </div>
            </div>
        </van-tab>
        <van-icon name="back-top" class="toTop" v-show="flag" @click="gotoTop" />
      </van-tabs>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import Cookies from 'js-cookie'
import { NavBar, Tab, Tabs, Card, Cell, CellGroup, Button, Dialog, Icon } from 'vant'
import { getOrder, pay, sign, delOrder } from '../../api/index.js'

Vue.use(Card)
Vue.use(Button)
Vue.use(Tab)
Vue.use(Tabs)
Vue.use(Cell)
Vue.use(CellGroup)
Vue.use(Cell)
Vue.use(CellGroup)
Vue.use(Icon)
// Vue.use(Dialog)
// Vue.use(ContactCard)
// Vue.use(ContactList)
// Vue.use(ContactEdit)
Vue.use(NavBar)
export default {
  data () {
    return {
      orderList: [],
      orderList1: [],
      orderList2: [],
      orderList3: [],
      orderList4: [],
      orderList5: [],
      active: 1,
      flag: '',
      flag3: ''
    }
  },
  methods: {
    gotoTop () {
      this.$refs.cont.scrollTop = 0
    },
    // 跳转到订单详情
    orderDetail (id) {
      this.$router.push('/orderDetail/' + id)
    },
    goSign (id) {
      sign({
        userId: Cookies.get('userId'),
        orderId: id
      }).then(() => {
        this.$router.go(0)
      })
    },
    // 去付款
    goPay (id) {
      pay({
        userId: Cookies.get('userId'),
        orderId: id
      }).then(() => {
        // go为0的时候刷新当前页面
        this.$router.go(0)
      })
      // 当已付款的时候则修改商品状态
    },
    // 计算某条订单的总价
    orderSumPrice (id) {
      let i = 0
      // 获取订单数据中的某个订单数据
      this.orderList.some((val, idx) => {
        i = idx
        return val.orderId === id
      })
      // 进如到该条订单数据 计算价格
      // reduce第二个参数是prev的默认值
      return this.orderList[i].proData.reduce((prev, next) => {
        return prev + (next.price - 0) * next.num
      }, 0)
    },
    // 取消的当条订单
    cancelOrder (id) {
      Dialog.confirm({
        title: '取消订单',
        message: '您确定要取消该订单？'
      }).then(() => {
        delOrder({
          userId: Cookies.get('userId'),
          orderId: id
        }).then(res => {
          this.$router.go(0)
        })
      }).catch(() => {
      })
    },
    onClickLeft () {
      this.$router.back()
    }
  },
  // 页面挂载完后获取数据
  mounted () {
    // 监听浏览器的滚动事件
    const that = this
    this.$refs.cont.addEventListener('scroll', function () {
      console.log(that.$refs.cont.scrollTop)
      if (that.$refs.cont.scrollTop > 300) {
        that.flag = true
      } else {
        that.flag = false
      }
    })
    // 获取订单数据
    getOrder({
      userId: Cookies.get('userId')
    }).then(res => {
      console.log(res)
      this.orderList = res.data.data
      // console.log(this.orderList)
      // 0 待付款 1 待发货 2 待收货 3 待评价 4 已完成
      res.data.data.forEach(val => {
        if (val.state === 0) {
          this.orderList1.push(val)
          this.flag1 = '待付款'
        } else if (val.state === 1) {
          this.orderList2.push(val)
          this.flag2 = '待发货'
        } else if (val.state === 2) {
          this.orderList3.push(val)
        } else if (val.state === 3) {
          this.orderList4.push(val)
          this.flag3 = '待签收'
        } else if (val.state === 4) {
          this.orderList5.push(val)
          this.flag4 = '已完成'
        }
      })
    })
  }
}
</script>

<style lang="scss">
.header{
  border-bottom: 1px solid #aaa;
}
.order{
    padding: 0 0.12rem;
  .cont{
    overflow-y: auto;
    margin-top: 0.15rem;
  }
  .van-cell-group{
    margin-bottom: 0.2rem;
  }
  .van-cell{
    border-bottom: 1px solid #ccc;
    padding-top: 0.1rem;
    .van-cell__title{
      padding: 0.03rem;
    }
    .van-cell__value{
        .evaluate{
            background-color: normal;
            border: 1px solid #26cad3;
            padding: 0 0.05rem;
            color: #26cad3;
        }
    }
    .van-button{
      padding: 0;
    }
    .van-tabs{
      position: fixed;
    }
    .recipient{
      padding: 0.03rem 0;
    }
  }
  .orderNum{
    font-size: 0.16rem;
    display: flex;
    justify-content: space-between;
    color: #000;
    .num{
      margin-left: 8px;
    }
    .statePay{
      color: #f40;
    }
  }
  .van-card{
    margin-top: 3px;
  }
  .tabpanel{
    margin-top: 5px;
  }
  .order-footer{
    position: relative;
    margin-top: 0.05rem;
    display: flex;
    justify-content: space-between;
    .order-sum{
      font-weight: 600;
      .order-money{
        color: #f50;
        font-style: normal;
        margin-left: 0.03rem;
      }
    }
    .gopay{
      width: 0.6rem;
      border-radius: 8px;
      margin-left: 0.1rem;
      padding: 0.05rem;
      background-color: #f40;
      color: #fff;
    }
    .esti{
      width: 0.6rem;
      border-radius: 0.10rem;
      padding: 0.05rem;
      background: none;
      color: #000;
      border: 1px solid #000;
    }
    .delOrder{
        background: none;
        color: #000;
        border: 1px solid #000;
        padding: 0.03rem;
        margin-left: 0.18rem;
    }
    .sign-right{
        .van-button--primary{
          background-color: transparent;
          border: 1px solid #26cad3;
          color: #26cad3;
        }
    }
  }
  .toTop{
        position: fixed;
        bottom: 1rem;
        right: 0.4rem;
        font-size: 0.3rem;
        background: #fff;
        padding: 0.1rem;
        border-radius: 6px;
        border: solid 2px #eee;
      }
}
</style>
